var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    winHeight: "", //窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置

    date: null, //自定义
    date_day: null,
    date_week: null,
    date_month: null,

    date1: null,
    date2: null,

    flag: 'day',

    week_date: '',
    week_day: '', //一年中第几星期



    nowTime: '',
    nowMonth: '',

    open: false,


    pj_week_start: '',
    pj_week_end: '',



    //周选择器
    index: 0,
    customItem: '全部',
    week_date: [],
    year_date: [2018, 2017, 2016, 2015],
    change_week: '',
    change_year: ''

  },
  //生成周
  bindPickerChange_year: function(e) {
    var that = this;
    console.log('picker发送选择改变，携带值为', this.data.year_date[e.detail.value])
    this.setData({
      index: e.detail.value,
      change_year: this.data.year_date[e.detail.value] //选中的年份
    })
    console.log(this.data.change_year)




    var week = function(year) {
      var d = new Date(year, 0, 1);
      while (d.getDay() != 1) {
        d.setDate(d.getDate() + 1);
      }
      var to = new Date(year + 1, 0, 1);
      var i = 1;

      var str = [];
      for (var from = d; from < to;) {
        str += `第${(i>9?i:'0'+i)}周 ${(from.getMonth() + 1) > 9 ? (from.getMonth() + 1) : '0' + (from.getMonth() + 1)}/${(from.getDate()) > 9 ? (from.getDate()): '0' + (from.getDate())}-`;
        from.setDate(from.getDate() + 6);
        if (from < to) {
          str += `${(from.getMonth() + 1) > 9 ? (from.getMonth() + 1) : '0' + (from.getMonth() + 1)}/${(from.getDate()) > 9 ? (from.getDate()) : '0' + (from.getDate())}*`;
          from.setDate(from.getDate() + 1);
        } else {
          to.setDate(to.getDate() - 1);
          str += `${(to.getMonth() + 1) > 9 ? (to.getMonth() + 1) : '0' + (to.getMonth() + 1)}/${(to.getDate()) > 9 ? (to.getDate()) : '0' + (to.getDate())}*`;
        }
        i++;
      }
      var arr = str.split('*')
      // console.log(arr)
      // console.log(str)

      that.setData({
        week_date: arr
      })
    }
    week(this.data.change_year);
  },
  bindPickerChange_week: function(e) {
    if (this.data.change_year == '') {
      wx.showToast({
        title: '小主请先选择年份',
        icon: "none"
      })
    } else {
      console.log('picker发送选择改变，携带值为', this.data.week_date[e.detail.value])
      var date = new Date();
      var nowtime = date.getFullYear() + '/' + ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + '/' + (date.getDate() > 9 ? (date.getDate()) : '0' + (date.getDate()))
      var test = Date.parse(nowtime) / 1000

      this.setData({
        index: e.detail.value,
        change_week: this.data.week_date[e.detail.value],

      })

      var pj_week_start = this.data.change_year + '/' + (this.data.week_date[e.detail.value]).substring(5, 10)
      var pj_week_end = this.data.change_year + '/' + (this.data.week_date[e.detail.value]).substring(11, 16)

      var date1 = new Date(pj_week_start)
      var sjc1 = date1.getTime() / 1000
      var date2 = new Date(pj_week_end)
      var sjc2 = date2.getTime() / 1000
      if (sjc1 < test) {
        // app.globalData.start_date = sjc1;
        // app.globalData.end_date = sjc2;
        this.setData({
          open: true,
          pj_week_start: sjc1,
          pj_week_end: sjc2
        })
      } else {
        wx.showToast({
          title: '不能选择未来时间噢~',
          icon: 'none'
        })
      }
      console.log('sjc1+' + sjc1)
      console.log('sjc1+' + test)




      //  }else{
      //    wx.showToast({
      //      title: '小主，周期不能大于当前周哦~',
      //      icon:'none'
      //    })
      //    return false;
      //  }

    }


  },





  //确认时间选择
  click_time: function() {

    if (this.data.flag == 'day') {
      console.log(this.data.date_day)
      var s_day = this.data.date_day;
      var pj_day = s_day.split('-')
      var newArr1 = pj_day.splice(3, 0, ' 00:00:00')
      var pj_day1 = pj_day.join("/")

      var lastStr1 = (pj_day1.slice(0, 10) + pj_day1.slice(11, 20))
      var date1 = new Date(lastStr1)
      var sjc1 = date1.getTime() / 1000

      console.log(sjc1)
      app.globalData.start_date = sjc1;
      app.globalData.end_date = sjc1;

      //选择判断  天
      if (this.data.date_day) {
        app.globalData.flag = "day"
        wx.reLaunch({
          url: '../report/report',
        })

      } else {
        wx.showToast({
          title: '小主~要选时间哦~',
          icon: 'none'
        })
      }
    } else if (this.data.flag == 'week') {


      // app.globalData.start_date = sjc1;
      // app.globalData.end_date = sjc2;

      //选择判断  周
      if (this.data.change_year && this.data.open) {
        app.globalData.start_date = this.data.pj_week_start;
        app.globalData.end_date = this.data.pj_week_end;
        app.globalData.flag = "week"
        wx.reLaunch({
          url: '../report/report',
        })

      } else {
        wx.showToast({
          title: '小主~要选对时间哦~',
          icon: 'none'
        })
      }

    } else if (this.data.flag == 'month') {
      var s_month = this.data.date_month;
      var pj_month1 = s_month.split('-')
      var pj_str = pj_month1.join('-')
      var pj_month = s_month.split('-')[1]
      var pj_year = s_month.split('-')[0]

      app.globalData.pj_month = pj_month; //设置全局月份
      app.globalData.pj_year = pj_year; //设置全局年份



      //开始判断
      switch (pj_month) {
        case "01":
          console.log("大月")
          console.log(pj_month)

          //月初
          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(newStr1)
          console.log(sjc1)
          app.globalData.start_date = sjc1


          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '31 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "03":
          console.log("大月")
          console.log(pj_month)

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1

          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '31 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "05":
          console.log("大月")
          console.log(pj_month)

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1

          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '31 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "07":
          console.log("大月")
          console.log(pj_month)

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1

          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '31 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "08":
          console.log("大月")
          console.log(pj_month)

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1

          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '31 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "10":
          console.log("大月")
          console.log(pj_month)

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1

          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '31 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "12":
          console.log("大月")
          console.log(pj_month)

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1

          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '31 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "04":
          //月 第一天
          console.log("小月")

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1


          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '30 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "06":
          //月 第一天
          console.log("小月")

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1


          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '30 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "09":
          //月 第一天
          console.log("小月")

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1


          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '30 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "11":
          //月 第一天
          console.log("小月")

          var oldArr1 = pj_str.split('-')
          var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
          var newStr1 = oldArr1.join('/')

          var date1 = new Date(newStr1)
          var sjc1 = date1.getTime() / 1000
          console.log(sjc1)
          app.globalData.start_date = sjc1


          //月 最后一天
          var oldArr2 = pj_str.split('-')
          var newArr2 = oldArr2.splice(2, 0, '30 00:00:00')
          var newStr2 = oldArr2.join('/')

          var date2 = new Date(newStr2)
          var sjc2 = date2.getTime() / 1000
          console.log(sjc2)
          app.globalData.end_date = sjc2

          break;

        case "02":
          console.log("二月")
          var s_other = this.data.date_month;
          var pj_year = s_other.split('-')[0];
          var pj_month = s_other.split('-')[1];



          console.log(pj_month)
          if ((pj_year % 4 == 0) && (pj_year % 100 != 0 || pj_year % 400 == 0)) {
            //月初
            var oldArr1 = pj_str.split('-')
            var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
            var newStr1 = oldArr1.join('/')

            var date1 = new Date(newStr1)
            var sjc1 = date1.getTime() / 1000
            console.log(sjc1)
            app.globalData.start_date = sjc1

            //月 最后一天
            var oldArr2 = pj_str.split('-')
            var newArr2 = oldArr2.splice(2, 0, '29 00:00:00')
            var newStr2 = oldArr2.join('/')

            var date2 = new Date(newStr2)
            var sjc2 = date2.getTime() / 1000
            console.log(sjc2)
            app.globalData.end_date = sjc2

          } else {


            //月初
            var oldArr1 = pj_str.split('-')
            var newArr1 = oldArr1.splice(2, 0, '01 00:00:00')
            var newStr1 = oldArr1.join('/')

            var date1 = new Date(newStr1)
            var sjc1 = date1.getTime() / 1000
            console.log(sjc1)
            app.globalData.start_date = sjc1

            //月 最后一天
            var oldArr2 = pj_str.split('-')
            var newArr2 = oldArr2.splice(2, 0, '28 00:00:00')
            var newStr2 = oldArr2.join('/')

            var date2 = new Date(newStr2)
            var sjc2 = date2.getTime() / 1000
            console.log(sjc2)
            app.globalData.end_date = sjc2

            // console.log(app.globalData.end_date)

          }
      }
      //选择判断  月
      if (this.data.date_month) {
        app.globalData.flag = "month"
        wx.reLaunch({
          url: '../report/report',
        })
      } else {
        wx.showToast({
          title: '小主~要选时间哦~',
          icon: 'none'
        })
      }
      //结束判断
    } else if (this.data.flag == 'other') {
      // console.log("走other")
      if (this.data.date1 == '') {

        wx.showToast({
          title: '开始时间不能为空',
          icon: "none"
        })
        return false;
      } else if (this.data.date2 == '') {
        wx.showToast({
          title: '结束时间不能为空',
          icon: "none"
        })
        return false;
      } else {
        console.log("成功")

        //开始时间
        var start_t = this.data.date1
        // console.log(start_t)

        var pj_start = start_t.split('-')
        var newArr1 = pj_start.splice(3, 0, ' 00:00:00')
        var pj_start1 = pj_start.join("/")

        var lastStr1 = (pj_start1.slice(0, 10) + pj_start1.slice(11, 20))
        var date1 = new Date(lastStr1)
        var sjc1 = date1.getTime() / 1000
        // console.log(sjc1)

        //结束时间
        var end_t = this.data.date2
        // console.log(end_t)

        var pj_end = end_t.split('-')
        var newArr1 = pj_end.splice(3, 0, ' 00:00:00')
        var pj_end1 = pj_end.join("/")

        var lastEnd1 = (pj_end1.slice(0, 10) + pj_end1.slice(11, 20))
        var date2 = new Date(lastEnd1)
        var sjc2 = date2.getTime() / 1000

        //判断结束时间是否大于开始时间
        if (sjc2 >= sjc1) {
          if (sjc2 - sjc1 < 86400 * 100) {
            app.globalData.start_date = sjc1;
            app.globalData.end_date = sjc2;

            console.log(app.globalData.start_date)
            console.log(app.globalData.end_date)

            //跳转
            app.globalData.flag = "other"
            wx.reLaunch({
              url: '../report/report',
            })

          } else {
            wx.showToast({
              title: '小主~天数不能大于100天呢~',
              icon: 'none'
            })
            return false;
          }

        } else {
          wx.showToast({
            title: '小主~结束时间必须大于开始时间呢~',
            icon: 'none'
          })
          return false;
        }





      }

    }
    //选择判断  其他
  },




  //日
  bindDateChange_day: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date_day: e.detail.value
    })
  },
  //周
  bindDateChange_week: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date_week: e.detail.value
    })
  },
  //月
  bindDateChange_month: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date_month: e.detail.value
    })
  },
  //自定义开始
  bindDateChange_1: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    // console.log(e)
    this.setData({
      date1: e.detail.value
    })
  },
  //自定义结束
  bindDateChange_2: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date2: e.detail.value
    })
  },



  //禁止滑动
  catchTouchMove: function(res) {
    return false
  },
  // 滚动切换标签样式
  switchTab: function(e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
    if (e.detail.current == 0) { //日数据
      this.setData({
        flag: "day",
        date_day: ''
      })


    } else if (e.detail.current == 1) { //周数据
      this.setData({
        flag: "week",
        date_week: ''
      })

    } else if (e.detail.current == 2) { //月数据
      this.setData({
        flag: "month",
        date_month: ''
      })

    } else if (e.detail.current == 3) { //自定义
      this.setData({
        flag: 'other',
        date2: '',
        date1: ''
      })

    } else {}
    console.log(this.data.flag)
    console.log('app+' + app.globalData.flag)


  },
  // 点击标题切换当前页时改变样式
  swichNav: function(e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function() {
    if (this.data.currentTab > 2) {
      this.setData({
        scrollLeft: 1200
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function() {
    var that = this;

    var myDate = new Date();
    // console.log(util.formatTime(new Date()));
    console.log(myDate.getDay());




    //  高度自适应
    wx.getSystemInfo({
      success: function(res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    //生成周


    var week = function(year) {
      var d = new Date(year, 0, 1);
      while (d.getDay() != 1) {
        d.setDate(d.getDate() + 1);
      }
      var to = new Date(year + 1, 0, 1);
      var i = 1;

      var str = [];
      for (var from = d; from < to;) {
        str += `第${(i>9)?i:'0'+i}周 ${(from.getMonth() + 1) > 9 ? (from.getMonth() + 1) : '0' + (from.getMonth() + 1)}/${(from.getDate()) > 9 ? (from.getDate()) : '0' + (from.getDate())}-`;
        from.setDate(from.getDate() + 6);
        if (from < to) {
          str += `${(from.getMonth() + 1) > 9 ? (from.getMonth() + 1) : '0' + (from.getMonth() + 1)}/${(from.getDate()) > 9 ? (from.getDate()) : '0' + (from.getDate())}*`;
          from.setDate(from.getDate() + 1);
        } else {
          to.setDate(to.getDate() - 1);
          str += `${(to.getMonth() + 1) > 9 ? (to.getMonth() + 1) : '0' + (to.getMonth() + 1)}/${(to.getDate()) > 9 ? (to.getDate()) : '0' + (to.getDate())}*`;
        }
        i++;
      }
      var arr = str.split('*')
      // console.log(arr)
      // console.log(str)

      that.setData({
        week_date: arr
      })
    }
    week(this.data.change_year);


    // console.log(d.showWeek());
    //结束
  },
  onShow: function() {
    // app.globalData.flag = "day" //解决周回日,还是显示周的问题

    var date = new Date();
    var nowtime = date.getFullYear() + '-' + ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + '-' + (date.getDate() > 9 ? (date.getDate()) : '0' + (date.getDate()))
    var nowmonth = date.getFullYear() + '-' + ((date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1))
    this.setData({
      nowTime: nowtime,
      nowMonth: nowmonth
    })
    console.log(app.globalData.start_date)

    //计算星期几
    var d1 = new Date();
    var d2 = new Date();
    d2.setMonth(0);
    d2.setDate(1);
    var rq = d1 - d2;
    var s1 = Math.ceil(rq / (24 * 60 * 60 * 1000));
    var s2 = Math.ceil(s1 / 7);
    this.setData({
      week_day: s2
    })








  },
  footerTap: app.footerTap
})